package oppsa.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;
import oppsa.controller.Translate;
import oppsa.model.ActionDB;
import oppsa.model.ProjectDB;
import oppsa.model.SSComponentDB;
import oppsa.util.Session;

import org.jdesktop.swingx.JXFrame;

public class RenameSSComponentBox extends JXFrame {
	private static final long serialVersionUID = 1L;
	private JTextField textFieldSSComponent;
	private JPanel panel;
	private JLabel lblAddSSComponent;
	private JLabel lblSSComponent;
	private JButton btnSubmit;
	private SSComponentDB listSSComponent;
	private ActionDB action;
	private JButton source;
	private JButton btnCancel;
	private HomeView hv;

	/**
	 * @wbp.parser.constructor
	 */
	public RenameSSComponentBox(final HomeView hv) {
		addWindowListener(closeWindow);
		setIconImage(Toolkit.getDefaultToolkit().getImage(RenameSSComponentBox.class.getResource("/LOGO_FONCE.png")));
		this.hv = hv;
		this.listSSComponent = new SSComponentDB();
		this.action = new ActionDB();
		final Translate t = new Translate();
		
		

		
		hv.addWindowFocusListener(clickhomeview);
		setStartPosition(StartPosition.CenterInScreen);
		getRootPaneExt().getContentPane().setBackground(Color.RED);
		setMinimumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setResizable(false);

		setTitle(t.getLocalizedText("tlRenameSubSystemComponent"));
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));
		lblAddSSComponent = new JLabel(t.getLocalizedText("lblRenameSubSystemComponent"));
		lblAddSSComponent.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddSSComponent, "cell 0 0");
		
		lblSSComponent = new JLabel(t.getLocalizedText("lblRenameSubSystemComponent"));
		lblSSComponent.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel.add(lblSSComponent, "cell 0 2,alignx left");
		listSSComponent.selectByName(RenameSSComponentBox.this.hv.getSelectedSSComp(), RenameSSComponentBox.this.hv.getSelectedSSCompParent());
		
		textFieldSSComponent = new JTextField(listSSComponent.getFullList().get(0).getSsComponentName());
		panel.add(textFieldSSComponent, "cell 1 2,alignx left");
		textFieldSSComponent.setColumns(20); 

		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Validate"))));
		btnSubmit.setBorder(null);
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (textFieldSSComponent.getText().equals("")) {
					textFieldSSComponent.setBackground(Color.RED);
					JOptionPane.showMessageDialog(RenameSSComponentBox.this, "Aucune valeur saisie");
				}else{
					
					listSSComponent.selectByName(RenameSSComponentBox.this.hv.getSelectedSSComp(), RenameSSComponentBox.this.hv.getSelectedSSCompParent());
					int seq = listSSComponent.getFullList().get(0).getSequence();
					ProjectDB p = new ProjectDB();
					p.select(Session.userSequence);
					listSSComponent.renameSSCompBySeq(seq, textFieldSSComponent.getText());
					action.updateActionIDbyMeeting(seq,false);
					RenameSSComponentBox.this.hv.refreshTree2();
					hv.setVisible(true);
					hv.setEnabled(true);
					hv.toFront();
					setVisible(false);
					
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});
		

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				hv.setVisible(true);
				hv.setEnabled(true);
				hv.toFront();
				setVisible(false);
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
			}
		});
		


		
		panel.add(btnCancel, "cell 1 4");
		panel.add(btnSubmit, "cell 1 4");
	}
	
	WindowAdapter closeWindow = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			hv.setVisible(true);
			hv.setEnabled(true);
			hv.toFront();
		}
	};
	
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

		@Override
		public void windowGainedFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			toFront();
		}

		@Override
		public void windowLostFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	};
}
